package com.education.project.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.education.project.model.LoginUserType;
import com.education.project.service.LoginUserTypeService;
import com.education.project.service.WebNavResourcesService;

@Controller
@RequestMapping("/system/loginusertype")
public class LoginUserTypeController {
	@Autowired
	private LoginUserTypeService loginUserTypeService;
	@Autowired
	private WebNavResourcesService webNavResourcesService;
	
	@RequestMapping(value="/index",method=RequestMethod.GET)
	public String getIndexPage() {
		return "loginusertype/index";
	}
	
	@RequestMapping(value="/index",method=RequestMethod.POST)
	public @ResponseBody List getall() {
		return loginUserTypeService.getAllLoginUserType();
	}
	
	@RequestMapping(value="/get_user_type_manager_tree",method=RequestMethod.POST)
	public @ResponseBody List getUserTypeManagerTree(@RequestParam("usertypeid") Integer loginUserTypeId) {
		return webNavResourcesService.getResourcesByUserTypeTree(loginUserTypeId);
	}
	
	@RequestMapping(value="/updateusertypenav",method=RequestMethod.POST)
	public @ResponseBody Boolean updateUserTypeNav(
			@RequestParam("loginUserTypeId") Integer loginUserTypeId,
			@RequestParam("navstr") String navstr) {
		List<Map> list = new ArrayList<Map>();
		if(navstr.length()>0) {
			String[] str = navstr.split(",");
			for(String nav:str) {
				Integer navresourcesid = Integer.parseInt(nav);
				Map map = new HashMap();
				map.put("loginUserTypeId", loginUserTypeId);
				map.put("navresourcesid", navresourcesid);
				list.add(map);
			}
		}
		loginUserTypeService.updateUserTypeNav(loginUserTypeId, list);
		return true;
	}
	
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public @ResponseBody Map addLoginUser(@Valid @ModelAttribute("loginUserType") LoginUserType loginUserType,
			BindingResult binder) {
		Map result = new HashMap();
		if(binder.hasErrors()) {
			FieldError fieldError = binder.getFieldErrors().get(0);
			result.put("success", false);
			result.put("message", fieldError.getDefaultMessage());
			return result;
		}
		loginUserTypeService.saveLoginUserType(loginUserType);
		result.put("success", true);
		return result;
	}
	
	@RequestMapping(value="/update",method=RequestMethod.POST)
	public @ResponseBody Map updateLoginUser(@Valid @ModelAttribute("loginUserType") LoginUserType loginUserType,
			BindingResult binder) {
		Map result = new HashMap();
		if(binder.hasErrors()) {
			FieldError fieldError = binder.getFieldErrors().get(0);
			result.put("success", false);
			result.put("message", fieldError.getDefaultMessage());
			return result;
		}
		LoginUserType userType = loginUserTypeService.getLoginUserType(loginUserType.getId());
		if(userType!=null) {
			userType.setTypename(loginUserType.getTypename());
			loginUserTypeService.updateLoginUserType(userType);
		}
		result.put("success", true);
		return result;
	}
	
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	public @ResponseBody Map removeLoingUserType(@RequestParam("id") Integer id) {
		Map result = new HashMap();
		Integer count = loginUserTypeService.getLoginUserCountByUserType(id);
		if(count.intValue()>0) {
			result.put("success", false);
			result.put("message", "该用户类别无法删除,还有其他用户引用该类别");
			return result;
		}
		Integer count1 = webNavResourcesService.getNavResourcesCountByUserType(id);
		if(count1.intValue()>0) {
			result.put("success", false);
			result.put("message", "该用户类别无法删除,还有其他资源引用该类别");
			return result;
		}
		loginUserTypeService.removeLoginUserType(id);
		result.put("success", true);
		return result;
	}
}
